//void, Obj This

Building this;
ObjList ol;
Unit uRecruit;
int i;
int nSleepTime = 50000;
str class_unit;

Sleep(1380+rand(GetConst("RandTimeStart"))+GetConst("AddTimeStart"));

this = This.AsBuilding();

if (!.IsValid()) return; 
//pr("dziala");
if(.race==Germany || .race==Gaul){
	while (.IsValid){ Sleep(60*60000); };
}

if (.race==Carthage){
   	//nSleepTime = 50000;
	class_unit = "CNoble";
} else if (.race==Iberia){
	class_unit = "ISlinger";
} else if (.race==Britain){
	class_unit = "BShieldBearer";
} else if (.race==Germany){
	class_unit = "TAxeman";
} else if (.race==Egypt){
	class_unit = "EArcherLongBow";
} else if (.race==Gaul){
	class_unit = "GAxeman";
} else if(.race == ImperialRome || .race == RepublicanRome){
	class_unit = "RGladiator";
}
         
while (.IsValid)
{
	if(EnvReadString(.settlement, "train_peasants") == "yes") {
		if (.race==Carthage){
			if(EnvReadString(.settlement, "Outpost Level 1") == "researched"){ 
				nSleepTime = 40000;
				if(EnvReadString(.settlement, "Outpost Level 2") == "researched"){
					nSleepTime = 25000;
				}
			}
		}
		ol = .settlement.Units.ObjClass("Peasant");
		ol.ClearDead();
		ol = ol.ObjAlly(.player);
		if (ol.count > 0)
		{ 
			i = rand(ol.count);
			if(ol[i].IsValid){
				if(ol[i].AsUnit.food > 0){
					ol[i].Erase;
					Sleep(rand(100)+100);                    
					uRecruit = Place(class_unit, .pos, .player);
					if(uRecruit.IsValid)
						.settlement.ForceAddUnit(uRecruit);
					
					ol.Clear();
				}
			}
		}
	}
	Sleep(nSleepTime);
}

